<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    IndexedMemcache
 * @subpackage Importer
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cCsvImporter {

    /**
     * import CSV File
     *
     * @param string $sClassName
     * @param string $sFile
     * @param string $sDelimiter
     * @return mixed count of imports or FALSE
     */
    public function csvImport($sClassName, $sFile, $sDelimiter = ',') {

        if (!class_exists($sClassName, TRUE)) {           
            return FALSE;
        }

        $aModelData = $this->getData($sFile, $sDelimiter);

        $aoMemCacheModels = $this->createObjects($sClassName, $aModelData);

        $this->saveObjects($aoMemCacheModels);

        return count($aoMemCacheModels);
    }

    /**
     * save MemCacheModel
     *
     * @param array $aoMemCacheModels
     */
    private function saveObjects($aoMemCacheModels) {
        foreach ($aoMemCacheModels as $oMemCacheModel) {
            cMemCacheFiles::saveObject($oMemCacheModel);
        }
    }


    /**
     * create MemCacheModel Objects
     *
     * @param string $sClassName
     * @param array $aModelData
     * @return array of MemCacheModels
     */
    private function createObjects($sClassName, $aModelData) {
        $aObjects = array();
        foreach ($aModelData as $aData) {
            $aObjects[] = $this->createObject($sClassName, $aData);
        }
        return $aObjects;
    }

    /**
     * create MemCacheObject
     *
     * @param string $sClassName
     * @param array $aModelData
     * @return cMemCacheModel
     */
    private function createObject($sClassName, $aModelData) {
        $oObject = new $sClassName;

        $oObject->setData($aModelData);

        return $oObject;
    }

    /**
     * get CSV Data as Array
     *
     * @param string $sFile
     * @param string $sDelimiter
     * @return array
     */
    private function getData($sFile, $sDelimiter) {
        if (!file_exists($sFile)) {
            return FALSE;
        }

        $oFileHandler = fopen ($sFile, 'r');
        if (!$oFileHandler) {
            return FALSE;
        }

        $aColumnNames = fgetcsv($oFileHandler, 4096, $sDelimiter);

        //loop through one row at a time
        $aModelData = array();
        while (($aData = fgetcsv($oFileHandler, 4096, $sDelimiter)) !== FALSE) {
            $aModelData[] = array_combine($aColumnNames, $aData);
        }

        fclose($oFileHandler);

        return $aModelData;
    }
}